package com.nova.bluetooth.netWork;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import com.nova.bluetooth.base.BaseApp;
import com.nova.bluetooth.helpUtils.LogUtil;
import com.nova.bluetooth.netWork.interceptor.MyHttpLoggingInterceptor;
import com.nova.bluetooth.netWork.ssl.SSLSocketFactoryCompat;

import java.security.cert.CertificateException;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;

public class RetrofitManage {
    private static final Object object = new Object();
    private static int TIME_OUT = 30; // 30秒超时断开连接
    public static int CHAT_GPT_TIME_OUT = 60 * 10; // 600秒超时断开连接 因为gpt回答问题比较慢
    private static int IMG_TIME_OUT = 120; // 120秒超时断开连接
    private static int REWRITE_TIME_OUT = 5 * 60; // 转写速度慢
    private static X509TrustManager trustAllCert = new X509TrustManager() {
        @Override
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
        }

        @Override
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[]{};
        }
    };

    private static SSLSocketFactory sslSocketFactory = new SSLSocketFactoryCompat(trustAllCert);

    private static final MyHttpLoggingInterceptor loggingInterceptor =
            new MyHttpLoggingInterceptor("RetrofitLog");
    //请求头添加token


    public static String getChannelNameFromAndroidManifest() {
        String channelName = "sanag";
        try {
            ApplicationInfo info = BaseApp.app.getPackageManager().getApplicationInfo(
                    BaseApp.app.getPackageName(),
                    PackageManager.GET_META_DATA);
            if (info.metaData == null) {
                return "sanag";
            }
            channelName = info.metaData.getString("ChannelName");
            LogUtil.d("channelName==>" + channelName);
        } catch (PackageManager.NameNotFoundException e) {
        }
        return channelName;
    }

    public static String getDefLanguage() {
        //获取当前语言，优先SharePreferenceUtils 本地的，没有就用系统
//        String language = (String) SharePreferenceUtils.get(BaseApp.app,
//                Constant.SHARE_PREFERENCE_SAVA_LANGUAGE, LanguageUtil.getSysLanguage());
//        KLog.d("language = " + language);
//        if (language.equals(LanguageUtil.CHINESE_CN) || language.equals(LanguageUtil.CHINESE_HANS_CN)) {
//            return "zh_CN";
//        } else if (language.equals(LanguageUtil.CHINESE_HK)) {
//            return "zh_HK";
//        } else if (language.equals(LanguageUtil.CHINESE_MO)) {
//            return "zh_MO";
//        } else if (language.startsWith(LanguageUtil.CHINESE_)) {
//            return "zh_TW";
//        } else if (language.startsWith(LanguageUtil.ENGLISH_UK)) {
//            return "en_GB";
//        } else if (language.startsWith(LanguageUtil.ENGLISH)) {
//            return "en_US";
//        } else if (language.startsWith(LanguageUtil.KOREAN)) {
//            return "ko_KR";
//        } else if (language.startsWith(LanguageUtil.RUSSIAN)) {
//            return "ru_RU";
//        } else if (language.startsWith(LanguageUtil.PORTUGUESE)) {
//            return "pt_PT";
//        } else if (language.startsWith(LanguageUtil.JAPANESE)) {
//            return "ja_JP";
//        } else if (language.startsWith(LanguageUtil.ARABIC_EGYPT)) {
//            return "ar_SA"; // 阿拉伯语，可以根据需要调整
//        } else if (language.startsWith(LanguageUtil.INONESIAN)) {
//            return "in_ID"; // 印尼语
//        } else if (language.startsWith(LanguageUtil.FRENCH)) {
//            return "fr_FR"; // 法语
//        } else {
//            return "zh_CN";
//        }

        return "zh_CN";
    }


    public static OkHttpClient createOkHttpClient(int timeout, Interceptor interceptor) {
        return new OkHttpClient.Builder()
                .sslSocketFactory(sslSocketFactory, trustAllCert)
                .connectTimeout(timeout, TimeUnit.SECONDS)
                .readTimeout(timeout, TimeUnit.SECONDS)
                .writeTimeout(timeout, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .addInterceptor(loggingInterceptor)
                .build();
    }

    public static OkHttpClient createSseOkHttpClient(int timeout) {
        return new OkHttpClient.Builder()
                .sslSocketFactory(sslSocketFactory, trustAllCert)
                .connectTimeout(timeout, TimeUnit.SECONDS)
                .readTimeout(timeout, TimeUnit.SECONDS)
                .writeTimeout(timeout, TimeUnit.SECONDS)
                .build();
    }

    private static Retrofit retrofit = null;
    private static Retrofit chatGptRetrofit = null;
    private static Retrofit imgRetrofit = null;

    public static synchronized Retrofit createRetrofit(String URL_BASE, Interceptor interceptor) {
        LogUtil.d("URL_BASE>>" + URL_BASE);
        synchronized (object) {
            if (retrofit == null) {
                retrofit = new Retrofit.Builder()
                        .baseUrl(URL_BASE)
                        .client(createOkHttpClient(TIME_OUT, interceptor))
                        .build();
            }
            return retrofit;
        }
    }


    public static synchronized Retrofit createChatGptRetrofit(String URL_BASE_CHAT_GPT, Interceptor interceptor) {
        LogUtil.d( "URL_BASE_CHAT_GPT>>" + URL_BASE_CHAT_GPT);
        synchronized (object) {
            if (chatGptRetrofit == null) {
                chatGptRetrofit = new Retrofit.Builder()
                        .baseUrl(URL_BASE_CHAT_GPT)
                        .client(createOkHttpClient(CHAT_GPT_TIME_OUT, interceptor))
                        .build();
            }
            return chatGptRetrofit;
        }
    }


    public static synchronized Retrofit createImgRetrofit(String URL_BASE_CHAT_GPT, Interceptor intercepto) {
        synchronized (object) {
            if (imgRetrofit == null) {
                imgRetrofit = new Retrofit.Builder()
                        .baseUrl(URL_BASE_CHAT_GPT)
                        .client(createOkHttpClient(IMG_TIME_OUT, intercepto))
                        .build();
            }
            return imgRetrofit;
        }
    }

    public static synchronized Retrofit createInvoiceRetrofit(String URL_BASE, Interceptor intercepto) {
        synchronized (object) {
            if (imgRetrofit == null) {
                imgRetrofit = new Retrofit.Builder()
                        .baseUrl(URL_BASE)
                        .client(createOkHttpClient(REWRITE_TIME_OUT, intercepto))
                        .build();
            }
            return imgRetrofit;
        }
    }
}
